 //
//  MBStoryController.m
//  mibaotu
//
//  Created by 彭康 on 16/9/14.
//  Copyright © 2016年 pegnk. All rights reserved.
//

#import "MBStoryController.h"
#import "MBSongDetailController.h"
#import "MBDetailViewController.h"
#import "MBSpaceAlbumViewController.h"
#import "MBStorySceneController.h"
#import "MBSpaceMyParentChildShowPlayViewController.h"
#import "MBLoginController.h"
#import "MBSongDetailForIController.h"

#import "MBStoryTabbarViewController.h"
#import "MBStorySearchViewController.h"
#import "MBStoryRecommendViewController.h"

#import "MBStoryRecommendCell.h"
#import "MBStoryAllListenCell.h"
#import "SharePLayerView.h"
#import "MBStoryAlbumAndAnchorCell.h"
#import "MBStorySceneCell.h"
#import "EScrollerView.h"
#import "CLShareManager.h"


#import "MBSongDetailForIController.h"
#import "MBSongDetailForIpadController.h"
#import "MBSpaceMyParentChildShowPlayViewForIpadController.h"
@interface MBStoryController ()<UITableViewDelegate,UITableViewDataSource,MBStoryAllListenCellDelegate,SharePLayerViewDelegate,MBStoryAlbumAndAnchorCellDelegate,MBStorySceneCellDelegate,EScrollerViewDelegate> {
    NSMutableArray                  *_dataArr;
    EScrollerView                   *escrollView;
    NSMutableArray                  *_hotAlbumListArr;  //热门专辑
    NSMutableArray                  *_hotAnchorListArr;  //人气主播
    NSMutableArray                  *_hotGetAlbumListArr;  //大家都在听
    NSMutableArray                  *_hotGetPicListArr;  //轮播
    NSMutableArray                  *_hotGetFansListArr;  //主编推荐
    NSMutableDictionary             *_selectHotAlbumDic; //大家都在听歌曲
    CLShareManager                  *shareView;          //分享
    NSMutableArray                  *fileIDArr;          //已下载的歌曲列表
    BOOL                            isAudioChange;      //歌曲是否改变
}

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MBStoryController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (ISIPAD) {
        [self onUpdateUIForView:self.orientation];
    }
    
    NSArray *downedMusicArr = [UserManager standerDefult].downedMusicArr;
    fileIDArr = [[NSMutableArray alloc]initWithCapacity:downedMusicArr.count];
    for (int i=0; i<downedMusicArr.count; i++) {
        NSDictionary *dic = [downedMusicArr objectAtIndex:i];
        NSString *fileID = [dic objectForKey:@"fileID"];
        [fileIDArr addObject:fileID];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _hotAlbumListArr = [[NSMutableArray alloc]init];
    _hotAnchorListArr = [[NSMutableArray alloc]init];
    _hotGetAlbumListArr = [[NSMutableArray alloc]init];
    _hotGetPicListArr = [[NSMutableArray alloc]init];
    _hotGetFansListArr = [[NSMutableArray alloc]init];
    _selectHotAlbumDic = [[NSMutableDictionary alloc]init];
    _dataArr = [[NSMutableArray alloc]initWithObjects:@[@{@"pic":@"erge_gsk",@"text":@"儿歌"},@{@"pic":@"gushi_gsk",@"text":@"故事"},@{@"pic":@"yingwen_gsk",@"text":@"英文"},@{@"pic":@"guoxue_gsk",@"text":@"国学"}],@[@{@"pic":@"0-2",@"text":@"0-2岁"},@{@"pic":@"3-4",@"text":@"3-4岁"},@{@"pic":@"5-6",@"text":@"5-6岁"},@{@"pic":@"6sui",@"text":@"6岁以上"}],@[@{@"pic":@"shuiqiananfu",@"text":@"睡前安抚"},@{@"pic":@"fumuxuetang",@"text":@"父母学堂"},@{@"pic":@"xixingyangcheng",@"text":@"习性养成"},@{@"pic":@"zhilikaifa",@"text":@"智力开发"}], nil];
    isAudioChange = NO;
    
    [self setNavBar];
    [self setupUI];
    
    [self loadData];
    
    [MobClick event:@"STORY_FIRST" label:@"故事库首页"];
}

#pragma mark - Actions
#pragma mark - UI
- (void)setNavBar
{
    [self setTitleView:@"米宝兔"];
    UIButton *lbt = [UIButton buttonWithType:UIButtonTypeCustom];
    lbt.frame = CGRectMake(0, 0, 60, 30);
    [lbt setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
    [lbt setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateHighlighted];
    [lbt setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 30)];
    [lbt addTarget:self action:@selector(onSelectItemForSearch) forControlEvents:UIControlEventTouchUpInside];
    [self setLeftBarItem:lbt];
    
    [self setRightPlayerItem];
}

- (void)setupUI {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = [self addTableHeaderView];
    
    [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
    
    //分享
    shareView = [[CLShareManager alloc] init];

}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.tableView.tableHeaderView = [self addTableHeaderView];
    [_tableView reloadData];
}

- (UIView *)addTableHeaderView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (ISIPAD?796*PERCEN:565*PERCEN))];
    bgView.backgroundColor = [UIColor clearColor];
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (ISIPAD?786*PERCEN:545*PERCEN))];
    contentView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:contentView];
    
    if (!escrollView) {
        escrollView = [[EScrollerView alloc]initWithFrameRect:CGRectMake(0, 0, contentView.width, (ISIPAD?500*PERCEN:300*PERCEN)) ImageArray:@[] TitleArray:@[] changPic:YES];
        escrollView.delegate = self;
    }
    escrollView.orientation = self.orientation;
    [contentView addSubview:escrollView];
    
    float itemWidth = 140*PERCEN;
    float itemLeft = (ISPortrait?38*PERCEN:(SCREEN_WIDTH-itemWidth*4)/5.);
    if (ISIPAD) {
        itemWidth = 160*PERCEN;
        itemLeft = (SCREEN_WIDTH-itemWidth*4)/5.;
    }
    for (int i=0; i<4; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(itemLeft+(itemLeft+itemWidth)*i, escrollView.bottom+30*PERCEN, itemWidth, itemWidth)];
        imgView.backgroundColor = [UIColor clearColor];
        imgView.layer.cornerRadius = imgView.height/2.;
        imgView.layer.masksToBounds = YES;
        [contentView addSubview:imgView];
        
        UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(imgView.left, escrollView.bottom+(ISIPAD?286*PERCEN-20-40*PERCEN:245*PERCEN-20-20*PERCEN), imgView.width, 20)];
        lb.backgroundColor = [UIColor clearColor];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.textColor = [UIColor blackColor];
        lb.font = [UIFont systemFontOfSize:13.];
        [contentView addSubview:lb];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(imgView.left, escrollView.bottom, imgView.width, 245*PERCEN);
        bt.backgroundColor = [UIColor clearColor];
        bt.tag = 8000+i;
        [bt addTarget:self action:@selector(onSelectItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:bt];
        
        if (i==0) {
            imgView.image = [UIImage imageNamed:@"zuixinshoufa_bg"];
            lb.text = @"最新首发";
        }
        else if (i==1) {
            imgView.image = [UIImage imageNamed:@"jingdianerge_bg"];
            lb.text = @"经典儿歌";
        }
        else if (i==2) {
            imgView.image = [UIImage imageNamed:@"renqigushi_bg"];
            lb.text = @"人气故事";
        }
        else if (i==3) {
            imgView.image = [UIImage imageNamed:@"changzhangbibei_bg"];
            lb.text = @"成长必备";
        }
    }
    
    return bgView;
}

//加载数据
- (void)loadData
{
    [self loadLunbo];
    [self loadFans];
}

- (void)loadLunbo
{
    //轮播图
    NSString *url = [NSString stringWithFormat:STORY_HOT_GETPIC_LIST_AUDIOLIST_URL_PATH,@1];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    [_hotGetPicListArr removeAllObjects];
                    [_hotGetPicListArr addObjectsFromArray:arr];
                    
                    NSMutableArray *mutPicArr = [[NSMutableArray alloc]initWithCapacity:_hotGetPicListArr.count];
                     NSMutableArray *mutPicArr2 = [[NSMutableArray alloc]initWithCapacity:_hotGetPicListArr.count];
                    for (NSDictionary *dic in _hotGetPicListArr) {
                        NSString *path = [dic objectForKey:@"FigurePath"];
                         NSString *path2 = [dic objectForKey:@"FigurePath2"];
                        [mutPicArr addObject:path];
                         [mutPicArr2 addObject:path2];
                    }
                    if (ISIPAD) {
                         [escrollView loadDataImageArray:mutPicArr2 TitleArray:@[] changPic:YES];
                    }else{
                        
                    [escrollView loadDataImageArray:mutPicArr TitleArray:@[] changPic:YES];
                    }
                    
                }
                [_tableView reloadData];
                [self loadHotAnchorList];
            }
        }
    } failure:^(NSError *error) {
        [self loadHotAnchorList];
    }];
}

- (void)loadFans
{
    //主编推荐
    NSString *url = [NSString stringWithFormat:STORY_FANS_ZHUBIAN_AUDIOLIST_URL_PATH,@1];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    [_hotGetFansListArr removeAllObjects];
                    [_hotGetFansListArr addObjectsFromArray:arr];
                }
                [_tableView reloadData];
                [self loadHotAlbumList];
            }
        }
    } failure:^(NSError *error) {
        [self loadHotAlbumList];
    }];

}

- (void)loadHotAlbumList
{
    //热门专辑
    NSString *url = [NSString stringWithFormat:STORY_HOT_ALBUM_LIST_AUDIOLIST_URL_PATH,@"1",@"",@"",@""];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    [_hotAlbumListArr removeAllObjects];
                    if (arr.count > 3) {
                        [_hotAlbumListArr addObject:arr[0]];
                        [_hotAlbumListArr addObject:arr[1]];
                        [_hotAlbumListArr addObject:arr[2]];
                    } else {
                        [_hotAlbumListArr addObjectsFromArray:arr];
                    }
                }
                [_tableView reloadData];
                [self loadHotAnchorList];
            }
        }
    } failure:^(NSError *error) {
        [self loadHotAnchorList];
    }];
}

- (void)loadHotAnchorList
{
    //人气主播
    NSString *url = [NSString stringWithFormat:STORY_HOT_ANCHOR_LIST_AUDIOLIST_URL_PATH,@"1"];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    [_hotAnchorListArr removeAllObjects];
                    if (arr.count > 3) {
                        [_hotAnchorListArr addObject:arr[0]];
                        [_hotAnchorListArr addObject:arr[1]];
                        [_hotAnchorListArr addObject:arr[2]];
                    } else {
                        [_hotAnchorListArr addObjectsFromArray:arr];
                    }
                }
                [_tableView reloadData];
                [self loadPeopleGetAudioList];
            }
        }
    } failure:^(NSError *error) {
        [self loadPeopleGetAudioList];
    }];
}

- (void)loadPeopleGetAudioList
{
    //大家都在听
    UserModel *model = [UserManager standerDefult].userModel;
    NSString *usrID = @"";
    if (model) {
        usrID = [NSString stringWithFormat:@"%@",model.UserID];
    }
    NSString *url = [NSString stringWithFormat:STORY_HOT_GETALBUM_LIST_AUDIOLIST_URL_PATH,usrID];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
        [_tableView.header endRefreshing];
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    [_hotGetAlbumListArr removeAllObjects];
                    [_hotGetAlbumListArr addObjectsFromArray:arr];
                }
                isAudioChange = YES;
                [_tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        [_tableView.header endRefreshing];
        [_tableView reloadData];
    }];
    
}

- (void)onLoadNew
{
    [self loadData];
}


#pragma mark - 搜索
- (void)onSelectItemForSearch
{
    MBStorySearchViewController *song = [[MBStorySearchViewController alloc]init];
    [self.navigationController pushViewController:song animated:YES];
}

#pragma mark - 播放器
- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

#pragma mark - 轮播图
-(void)EScrollerViewDidClicked:(NSUInteger)index
{
    NSLog(@"index:%d",(int)index);
    if (_hotGetPicListArr.count > index-1) {
        NSDictionary *dic = [_hotGetPicListArr objectAtIndex:index-1];
        if ([[dic objectForKey:@"FigureStatus"] intValue] == 0) return;
        
        MBSpaceAlbumViewController *song = [[MBSpaceAlbumViewController alloc]init];
        song.titleString = @"专辑";
        song.isFormStroy = YES;
        song.dateDic = dic;
        [self.navigationController pushViewController:song animated:YES];
    }
}

#pragma mark - 最新首发、经典儿歌、人气故事、成长必备
- (void)onSelectItemAction:(UIButton *)bt
{
    int index = (int)bt.tag - 8000;
    MBStoryTabbarViewController *barShow = [[MBStoryTabbarViewController alloc]init];
    if (index == 0) {
        barShow.type = MBStoryTabbarType_zuixinshoufa;
    }
    else if (index == 1) {
        barShow.type = MBStoryTabbarType_jingdianerge;
    }
    else if (index == 2) {
        barShow.type = MBStoryTabbarType_renqigushi;
    }
    else if (index == 3) {
        barShow.type = MBStoryTabbarType_chengzhangbibei;
    }
    [self.navigationController pushViewController:barShow animated:YES];
}

#pragma mark - 大家都在听
- (void)onSelectControlByCellWithValue:(NSDictionary *)value withView:(MBStoryAllListenCell *)view
{
    
    if ([value isKindOfClass:[NSDictionary class]]) {
//        if ([[value objectForKey:@"AudioStatus"] intValue] == 0) return;
        if (![_hotGetAlbumListArr containsObject:value]) return;
        [_selectHotAlbumDic removeAllObjects];
        [_selectHotAlbumDic addEntriesFromDictionary:value];
        
        int index = (int)[_hotGetAlbumListArr indexOfObject:value];
        
        if ([UserManager standerDefult].musicPlayType != MusicPLayType_DaJiaDouZaiTing) {
            //第一次
            [[UserManager standerDefult]playMusicWithMusicArray:_hotGetAlbumListArr withCurrentIndex:index withMusicPlayType:MusicPLayType_DaJiaDouZaiTing];
            isAudioChange = NO;
        }
        else if (isAudioChange == YES) {
            //换一批
            [[UserManager standerDefult]playMusicWithMusicArray:_hotGetAlbumListArr withCurrentIndex:index withMusicPlayType:MusicPLayType_DaJiaDouZaiTing];
             isAudioChange = NO;
        }
        else if ([UserManager standerDefult].selectMusicIndex != index) {
            //当前页面播放
            [[UserManager standerDefult]playMusicWithMusicArray:_hotGetAlbumListArr withCurrentIndex:index withMusicPlayType:MusicPLayType_DaJiaDouZaiTing];
        } else {
            //2次点击进入播放页面
            if ([UserManager standerDefult].musicArr.count > 0) {
                //歌曲列表没歌曲 无需进入
                UserManager *manager = [UserManager standerDefult];
                [manager setSelectMusicIndex:index];
                if (manager.audioPlayerType != AudioPLayerType_YinPin) {
                    [manager setIsPlaying:NO];
                    [manager setAudioPlayerType:AudioPLayerType_YinPin];
                } else {
                    [manager setIsPlaying:YES];
                }
//                if (ISIPAD) {
//                    MBSongDetailForIController*song=[[MBSongDetailForIController alloc]init];
//                    song.type=IdentityType_listen;
//                     [self.navigationController pushViewController:song animated:YES];
//                    return;
//                }
                if (ISIPAD) {
                    MBSongDetailForIpadController*song=[[MBSongDetailForIpadController alloc]init];
                    [self.navigationController pushViewController:song animated:YES];
                    return;
                }
                
                
                MBSongDetailController *song = [[MBSongDetailController alloc]init];
                [self.navigationController pushViewController:song animated:YES];

            }
        }
    }
}

- (void)onSelectControlByMoreButtonWithValue:(NSDictionary *)value withView:(MBStoryAllListenCell *)view
{
    if ([value isKindOfClass:[NSDictionary class]]) {
//        if ([[value objectForKey:@"AudioStatus"] intValue] == 0) return;
        if (![_hotGetAlbumListArr containsObject:value]) return;
        [_selectHotAlbumDic removeAllObjects];
        [_selectHotAlbumDic addEntriesFromDictionary:value];
        
        NSArray *collectionMusciArr = [UserManager standerDefult].collectionMusicArr;
        NSString *musicID = [NSString stringWithFormat:@"%@",[_selectHotAlbumDic objectForKey:@"ID"]];
        
        ShowPlayerType playerType = ShowPlayerType_StroyAllList;
        BOOL isCollection = NO;
        if ([collectionMusciArr containsObject:[_selectHotAlbumDic objectForKey:@"ID"]] || [collectionMusciArr containsObject:musicID]) {
            isCollection = YES;
        }
            
        if (isCollection == NO && ![fileIDArr containsObject:musicID]) {
            //未收藏，未下载
            playerType = ShowPlayerType_StroyAllList;
        }
        else  if (isCollection == NO && [fileIDArr containsObject:musicID]) {
            //未收藏，已下载
            playerType = ShowPlayerType_CollectionDowned;
        }
        else  if (isCollection == YES && ![fileIDArr containsObject:musicID]) {
            //已收藏，未下载
            playerType = ShowPlayerType_CancelCollectionDowned;
        }
        else  if (isCollection == YES && [fileIDArr containsObject:musicID]) {
            //已收藏，已下载
            playerType = ShowPlayerType_MusicMoew;
        }

        SharePLayerView *show = [[SharePLayerView alloc]initWithFrame:CGRectZero withType:playerType];
        show.delegate = self;
        show.orientation = self.orientation;
        [show onShowInWindow];
    }
}

#pragma mark - 大家都在听 播放、分享、收藏、下载、刷新
 //播放
- (void)onSelctItemForMorePlayMusic:(SharePLayerView *)view
{
    if ([_selectHotAlbumDic isKindOfClass:[NSDictionary class]]) {
//        if ([[_selectHotAlbumDic objectForKey:@"AudioStatus"] intValue] == 0) return;
        
        if (![_hotGetAlbumListArr containsObject:_selectHotAlbumDic]) return;
        
        int index = (int)[_hotGetAlbumListArr indexOfObject:_selectHotAlbumDic];
        
        if ([UserManager standerDefult].musicPlayType != MusicPLayType_DaJiaDouZaiTing) {
            //第一次
            [[UserManager standerDefult]playMusicWithMusicArray:_hotGetAlbumListArr withCurrentIndex:index withMusicPlayType:MusicPLayType_DaJiaDouZaiTing];
        }
        else if ([UserManager standerDefult].selectMusicIndex != index) {
            //当前页面播放
            [[UserManager standerDefult]playMusicWithMusicArray:_hotGetAlbumListArr withCurrentIndex:index withMusicPlayType:MusicPLayType_DaJiaDouZaiTing];
        } else {
            //2次点击进入播放页面
            if ([UserManager standerDefult].musicArr.count > 0) {
                //歌曲列表没歌曲 无需进入
                UserManager *manager = [UserManager standerDefult];
                [manager setSelectMusicIndex:index];
                if (manager.audioPlayerType != AudioPLayerType_YinPin) {
                    [manager setIsPlaying:NO];
                    [manager setAudioPlayerType:AudioPLayerType_YinPin];
                } else {
                    [manager setIsPlaying:YES];
                }
                
//                if (ISIPAD) {
//                    MBSongDetailForIController*song=[[MBSongDetailForIController alloc]init];
//                     [self.navigationController pushViewController:song animated:YES];
//                     return;
//                }
                if (ISIPAD) {
                    MBSongDetailForIpadController*song=[[MBSongDetailForIpadController alloc]init];
                    [self.navigationController pushViewController:song animated:YES];
                    return;
                }
                
                MBSongDetailController *song = [[MBSongDetailController alloc]init];
                [self.navigationController pushViewController:song animated:YES];
            }
        }
    }

}
//分享
- (void)onSelctItemForMoreShareMusic:(SharePLayerView *)view
{
     if (_selectHotAlbumDic.allKeys.count > 0) {
         NSString *audioName = [_selectHotAlbumDic objectForKey:@"AudioName"];
         audioName = [[audioName componentsSeparatedByString:@"."]firstObject];
         [shareView setShareVC:self content:[NSString stringWithFormat:@"分享:%@",audioName] image:[UIImage imageNamed:@"logo_dl_29"] url:[NSString stringWithFormat:SHARE_MUSIC_URL_PATH,[_selectHotAlbumDic objectForKey:@"ID"]]];
         [shareView addAudioId:[NSString stringWithFormat:@"%@",[_selectHotAlbumDic objectForKey:@"ID"]] withType:@1];
         [shareView show];
     }
}
//下载
- (void)onSelctItemForMoreDownMusic:(SharePLayerView *)view
{
    if (_selectHotAlbumDic.allKeys.count > 0) {
        [[UserManager standerDefult]downLoadMusic:_selectHotAlbumDic withRequestArr:fileIDArr];
        [ErrorMessage showErrorMessage:@"开始下载" inView:self.view];
    }
}
//收藏
- (void)onSelctItemForMoreCollectionMusic:(SharePLayerView *)view
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_COLLECION_URL_PATH,model.UserID,[_selectHotAlbumDic objectForKey:@"ID"],@1] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] integerValue]==0) {
            if ([_hotGetAlbumListArr containsObject:_selectHotAlbumDic]) {
                int index = (int)[_hotGetAlbumListArr indexOfObject:_selectHotAlbumDic];
                NSMutableDictionary *nDic = [[NSMutableDictionary alloc]initWithDictionary:_selectHotAlbumDic];
                [nDic setObject:@1 forKey:@"CollectStatus"];
                [_hotGetAlbumListArr replaceObjectAtIndex:index withObject:nDic];
                [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
                
                //本地数据同步
                if (![[UserManager standerDefult].collectionMusicArr containsObject:[_selectHotAlbumDic objectForKey:@"ID"]]) {
                    [[UserManager standerDefult].collectionMusicArr addObject:[_selectHotAlbumDic objectForKey:@"ID"]];
                }
                [[UserManager standerDefult]downLoadMusic:_selectHotAlbumDic withRequestArr:fileIDArr];
            }
            [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

//取消收藏
- (void)onSelctItemForMoreCancelMusicCollection:(SharePLayerView *)view
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_REMOVE_URL_PATH,model.UserID,[_selectHotAlbumDic objectForKey:@"ID"],@1] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] integerValue]==0) {
            if ([_hotGetAlbumListArr containsObject:_selectHotAlbumDic]) {
                int index = (int)[_hotGetAlbumListArr indexOfObject:_selectHotAlbumDic];
                NSMutableDictionary *nDic = [[NSMutableDictionary alloc]initWithDictionary:_selectHotAlbumDic];
                [nDic setObject:@0 forKey:@"CollectStatus"];
                [_hotGetAlbumListArr replaceObjectAtIndex:index withObject:nDic];
                [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
                
                //本地数据同步
                if ([[UserManager standerDefult].collectionMusicArr containsObject:[_selectHotAlbumDic objectForKey:@"ID"]]) {
                    [[UserManager standerDefult].collectionMusicArr removeObject:[_selectHotAlbumDic objectForKey:@"ID"]];
                }
            }
            [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

//刷新
- (void)onSelectReload
{
    [self loadPeopleGetAudioList];
}

#pragma mark - 热门专辑、人气主播
- (void)onSelectControlByType:(MBStoryAlbumAndAnchorType)type withView:(MBStoryAlbumAndAnchorCell *)view withValue:(NSString *)value
{
    if (![value isKindOfClass:[NSString class]]) return;
    
    
    if (type == MBStoryAlbumAndAnchorType_Album) {
        //专辑

        NSDictionary *dateDic = [_hotAlbumListArr objectAtIndex:0];
        if ([value intValue] == 1) {
            dateDic = [_hotAlbumListArr objectAtIndex:0];
        }
        else if ([value intValue] == 2) {
            dateDic = [_hotAlbumListArr objectAtIndex:1];
        }
        else if ([value intValue] == 3) {
            dateDic = [_hotAlbumListArr objectAtIndex:2];
        }
        MBSpaceAlbumViewController *song = [[MBSpaceAlbumViewController alloc]init];
        song.titleString = @"专辑";
        song.isFormStroy = YES;
        song.dateDic = dateDic;
        [self.navigationController pushViewController:song animated:YES];
    }
    else if (type == MBStoryAlbumAndAnchorType_Anchor) {
        //主播
#pragma mark - 人气主播播放器进入标准版本
        NSLog(@"主播");
        NSDictionary *dateDic = [_hotAnchorListArr objectAtIndex:0];
        if ([value intValue] == 1) {
            dateDic = [_hotAnchorListArr objectAtIndex:0];
        }
        else if ([value intValue] == 2) {
            dateDic = [_hotAnchorListArr objectAtIndex:1];
        }
        else if ([value intValue] == 3) {
            if (_hotAnchorListArr.count<3) {
                
                return;
            }
            dateDic = [_hotAnchorListArr objectAtIndex:2];
        }
        
        
        NSString *idStr ;
        UserManager *manager = [UserManager standerDefult];
        //每个主播都有若干个亲子秀，所以需要个判断是哪个主播，这个主播的第几首歌
        if (manager.audioPlayerType != AudioPLayerType_ZhuBo) {
            [[UserManager standerDefult].musicArr removeAllObjects];
            [[UserManager standerDefult]setSelectMusicIndex:0];
            
            //初次进入
            idStr = @"";
            manager.audioPlayerType = AudioPLayerType_ZhuBo;
        } else {
            //2次进入
            NSString *zhuboSelect = manager.zhubo_SelectIndex;
            if ([zhuboSelect isEqualToString:value]) {
                //同一个主播，判断是否同一个歌
                NSInteger selectIndex = manager.selectMusicIndex;
                NSArray *arr = manager.musicArr;
                Music *dics = [arr objectAtIndex:selectIndex];
                idStr = [NSString stringWithFormat:@"%@",dics.ID];
            } else {
                //不是同一个主播,相等于初次进入
                idStr = @"";
                [manager setSelectMusicIndex:0];
                [manager.musicArr removeAllObjects];
                [manager.mainPlayer pause];
            }
           
        }
        [manager setZhubo_SelectIndex:[NSString stringWithFormat:@"%@",value]];
        manager.zhuboDic = (NSMutableDictionary *)dateDic;
        
        if (ISIPAD) {
            MBSpaceMyParentChildShowPlayViewForIpadController*songDetailVC=[[MBSpaceMyParentChildShowPlayViewForIpadController alloc]init];
            songDetailVC.playTyle = MBSpaceMyParentChildShowPlayType_My;

            songDetailVC.dataDic = dateDic;
            songDetailVC.selectMusicID =idStr;
            [self.navigationController pushViewController:songDetailVC animated:YES];
            return;
        }

        
        MBSpaceMyParentChildShowPlayViewController *play = [[MBSpaceMyParentChildShowPlayViewController alloc]init];
        play.playTyle = MBSpaceMyParentChildShowPlayType_My;
        play.dataDic = dateDic;
        play.selectMusicID = idStr;
        [self.navigationController pushViewController:play animated:YES];
    }
}

- (void)onSelectRightMore:(UIButton *)bt
{
    if (bt.tag == 2100) {
        //热门专辑
        MBStorySceneController *scene = [[MBStorySceneController alloc]init];
        scene.titleString = @"热门专辑";
        scene.type = MBStorySceneCellType_AlbumList;
        scene.isAlbumForSearch = YES;
        [self.navigationController pushViewController:scene animated:YES];
    }
    else if (bt.tag == 2101) {
//        //人气主播
//        if (ISIPAD) {
//            MBSongDetailForIController*song=[[MBSongDetailForIController alloc]init];
//            song.type=IdentityType_hot;
//             [self.navigationController pushViewController:song animated:YES];
//            return;
//        }
//        
        MBStorySceneController *scene = [[MBStorySceneController alloc]init];
        scene.titleString = @"人气主播";
        scene.type = MBStorySceneCellType_HotAnchor;
        [self.navigationController pushViewController:scene animated:YES];
    }
}

#pragma mark - 分类、分龄、场景
- (void)onSelectControlByType:(MBStorySceneCellType)type withValue:(NSString *)value withView:(MBStorySceneCell *)view
{
    MBStorySceneController *scene = [[MBStorySceneController alloc]init];
    scene.type = type;
    scene.valueStr = value;
    [self.navigationController pushViewController:scene animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        //小编推荐
        static NSString *cellID = @"recommend_Cell";
        MBStoryRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBStoryRecommendCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.orientation = self.orientation;
        
        if (_hotGetFansListArr.count > 0) {
            cell.dataDic = _hotGetFansListArr[0];
        }
        
        return cell;
    }
    else if (indexPath.section == 1) {
        //大家都在ting
        static NSString *cellID = @"story_alllist_Cell";
        MBStoryAllListenCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBStoryAllListenCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.delegate = self;
        }
        cell.orientation = self.orientation;
        cell.musilArr = _hotGetAlbumListArr;
        return cell;
    }
    else if (indexPath.section >= 2 && indexPath.section <= 3) {
        //热门专辑 、人气主播
        static NSString *cellID = @"story_AlbumAndAnchor_Cell";
        MBStoryAlbumAndAnchorCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBStoryAlbumAndAnchorCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.delegate = self;
        }
        cell.orientation = self.orientation;
        
        if (indexPath.section == 2) {
            cell.type = MBStoryAlbumAndAnchorType_Album;
            cell.albumListArr = _hotAlbumListArr;
        }
        else if (indexPath.section == 3) {
            cell.type = MBStoryAlbumAndAnchorType_Anchor;
            cell.albumListArr = _hotAnchorListArr;
        } else {
            cell.type = MBStoryAlbumAndAnchorType_Album;
            cell.albumListArr = _hotAlbumListArr;
        }
        return cell;
    }
    else if (indexPath.section >= 4 && indexPath.section <=6) {
        
        static NSString *cellID = @"story_Scene_Cell";
        MBStorySceneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBStorySceneCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.delegate = self;
        }
        cell.orientation = self.orientation;
        
        if (indexPath.section == 4) {
            cell.type = MBStorySceneCellType_FenLei;
        }
        else if (indexPath.section == 5) {
            cell.type = MBStorySceneCellType_FenLing;
        }
        else if (indexPath.section == 6) {
            cell.type = MBStorySceneCellType_ChangJing;
        }
        
        if (_dataArr.count >= (indexPath.section-4)) {
            cell.dataArr = [_dataArr objectAtIndex:(indexPath.section-4)];
        }
        
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ISIPAD?70*PERCEN:50*PERCEN)];
    bgView.backgroundColor = [UIColor whiteColor];
    
    //白色线条
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake((ISIPAD?440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 0, 6*PERCEN, bgView.height)];
    lineView.backgroundColor = TINK_BG_COLOR;
    [bgView addSubview:lineView];
    
    UILabel *_nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(lineView.right+20*PERCEN, (bgView.height-20)/2., 200, 20)];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = TINK_BG_COLOR;
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [bgView addSubview:_nickNameLb];
    
    UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBt.frame = CGRectMake(bgView.width-120*PERCEN-(ISIPAD?440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 0, 120*PERCEN, bgView.height);
    rightBt.backgroundColor = [UIColor clearColor];
    [rightBt setTitle:@"换一批" forState:UIControlStateNormal];
    [rightBt setTitleColor:TINK_BG_COLOR forState:UIControlStateNormal];
    rightBt.titleLabel.font = [UIFont systemFontOfSize:12.f];
    [rightBt setImage:[UIImage imageNamed:@"shuaxin"] forState:UIControlStateNormal];
    if (ISIPAD) {
        [rightBt setTitleEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 0)];
        [rightBt setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 30)];
    } else {
        if (WIDTH_5S) {
            [rightBt setTitleEdgeInsets:UIEdgeInsetsMake(5, -5, 5, -5)];
            [rightBt setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 38)];
        } else {
            [rightBt setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0)];
            [rightBt setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 30)];
        }
    }
    [rightBt addTarget:self action:@selector(onSelectReload) forControlEvents:UIControlEventTouchUpInside];
    rightBt.hidden = YES;
    [bgView addSubview:rightBt];
    
    UIButton *rightMoreBt = [UIButton buttonWithType:UIButtonTypeCustom];
    rightMoreBt.frame = CGRectMake(bgView.width-140*PERCEN-(ISIPAD?440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 0, 140*PERCEN, bgView.height);
    rightMoreBt.backgroundColor = [UIColor clearColor];
    [rightMoreBt setTitle:@"查看更多>>" forState:UIControlStateNormal];
    [rightMoreBt setTitleColor:TINK_BG_COLOR forState:UIControlStateNormal];
    rightMoreBt.titleLabel.font = _nickNameLb.font;
    if (ISIPAD) {
        [rightMoreBt setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0)];
    } else {        
        if (WIDTH_5S) {
            [rightMoreBt setTitleEdgeInsets:UIEdgeInsetsMake(5, -5, 5, -5)];
            rightMoreBt.titleLabel.font = [UIFont systemFontOfSize:12.];
        } else {
            [rightMoreBt setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0)];
        }
    }
    [rightMoreBt addTarget:self action:@selector(onSelectRightMore:) forControlEvents:UIControlEventTouchUpInside];
    rightMoreBt.hidden = YES;
    [bgView addSubview:rightMoreBt];
    
    if (section == 0) {
        _nickNameLb.text = @"小编推荐";
    }
    else if (section == 1) {
        _nickNameLb.text = @"大家都在听";
        rightBt.hidden = NO;
    }
    else if (section == 2) {
        _nickNameLb.text = @"热门专辑";
        rightMoreBt.hidden = NO;
        rightMoreBt.tag = 2100;
    }
    else if (section == 3) {
        _nickNameLb.text = @"人气主播";
        rightMoreBt.hidden = NO;
        rightMoreBt.tag = 2101;
    }
    else if (section == 4) {
        _nickNameLb.text = @"分类";
        bgView.backgroundColor = TINK_BG_COLOR;
        lineView.backgroundColor = [UIColor whiteColor];
        _nickNameLb.textColor = [UIColor whiteColor];
    }
    else if (section == 5) {
        _nickNameLb.text = @"分龄";
        bgView.backgroundColor = TINK_BG_COLOR;
        lineView.backgroundColor = [UIColor whiteColor];
        _nickNameLb.textColor = [UIColor whiteColor];
    }
    else if (section == 6) {
        _nickNameLb.text = @"场景";
        bgView.backgroundColor = TINK_BG_COLOR;
        lineView.backgroundColor = [UIColor whiteColor];
        _nickNameLb.textColor = [UIColor whiteColor];
    }
    
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (ISIPAD?70*PERCEN:50*PERCEN);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20*PERCEN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return (_hotGetFansListArr.count == 0 ?0:160*PERCEN);
    }
    else if (indexPath.section == 1) {
        return _hotGetAlbumListArr.count*(ISIPAD?100*PERCEN:80*PERCEN);
    }
    else if (indexPath.section >= 2 && indexPath.section <= 3) {
        if (indexPath.section == 2) {
            return (_hotAlbumListArr.count == 0 ? 0 :(ISIPAD?284*PERCEN:264*PERCEN));
        }
        else if (indexPath.section == 3) {
            return (_hotAnchorListArr.count == 0 ? 0 :(ISIPAD?284*PERCEN:264*PERCEN));
        }
        return (ISIPAD?284*PERCEN:264*PERCEN);
    }
    else if (indexPath.section >= 4 && indexPath.section <= 6) {
        NSArray *arr = [_dataArr objectAtIndex:(indexPath.section-4)];
        return (arr.count == 0 ? 0: 264*PERCEN);
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MBStoryRecommendViewController *song = [[MBStoryRecommendViewController alloc]init];
        song.dataDic = _hotGetFansListArr[0];
        [self.navigationController pushViewController:song animated:YES];
    }
    else if (indexPath.section == 1) {
        
    }
    else if (indexPath.section >= 2 && indexPath.section <= 3) {
        
    }
    else if (indexPath.section >= 4 && indexPath.section <= 6) {
        
    }
}

@end
